package com.sport.common.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;

/**
 * Created by Kuzma on 2019/10/11.
 *
 * Descrivbe: 二维码生成配置
 *
 */
public class QRutil {

    public static int width = 200;
    public static int height = 200;

    public static String generateQRCodeBase64(String seatNumber) {
        // 二维码图片的宽度和高度
        int width = QRutil.width;
        int height = QRutil.height;

        // 二维码图片的格式
        String imageFormat = "png";

        // 创建二维码内容
        String qrCodeText = seatNumber;

        // 设置二维码参数
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        try {
            // 生成二维码图片
            BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, width, height, hintMap);
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);


            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, width, height);
            graphics.setColor(Color.BLACK);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    if (bitMatrix.get(x, y)) {
                        graphics.fillRect(x, y, 1, 1);
                    }
                }
            }
            // 给二维码下面添加对应座位的编号信号，让人容易分辨具体的座位
            // 在二维码下方添加座位编号
//            graphics.setColor(Color.BLUE);
            // 设置文本属性
            graphics.setColor(Color.BLACK);
//            graphics.setStroke(new BasicStroke(3));
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制文本
            FontMetrics metrics = graphics.getFontMetrics();
            int textWidth = metrics.stringWidth(seatNumber);
            graphics.drawString(seatNumber, (width-textWidth)/2, height - 10);

            // 释放资源
            graphics.dispose();
//            graphics.setFont(new Font("Arial", Font.PLAIN, 12));
//            FontMetrics metrics = graphics.getFontMetrics();
//            int textWidth = metrics.stringWidth(seatNumber);
//            int x = (width - textWidth) / 2;
//            int y = height + 20;
//            graphics.drawString(seatNumber, x, y);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, imageFormat, baos);
            // 将二维码图片转换为 Base64 编码的字符串
            byte[] imageBytes = baos.toByteArray();
            return Base64.encodeBase64String(imageBytes);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }


}
